package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.User;
import com.itheima.service.UserManageService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = UserManageService.class)
public class UserManageServiceImpl implements UserManageService {
 @Autowired
 private UserDao userDao;
 @Autowired
 private RoleDao roleDao;

  @Override
  public void deleteUser(Integer id) {
    try{
      userDao.deleteUserAndRole(id);
    }catch (Exception e){
      e.printStackTrace();
    }
    userDao.deleteUser(id);
  }

  @Override
  public PageResult findPage(QueryPageBean queryPageBean) {
    Integer currentPage = queryPageBean.getCurrentPage();
    Integer pageSize = queryPageBean.getPageSize();
    String queryString = queryPageBean.getQueryString();
    PageHelper.startPage(currentPage,pageSize);
    Page<User> result=userDao.selectByCondition(queryString);
    long total = result.getTotal();
    List<User> row = result.getResult();
    return new PageResult(total,row);
  }

  @Override
  public List<Integer> findRoleById(Integer id) {
    return roleDao.findRoleById(id);
  }
}
